/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sync.local;

import java.util.ArrayList;
import java.util.List;
import name.pachler.nio.file.WatchEvent.Kind;
import java.io.File;
import java.util.Map;
import configuration.ConfigurationManager;
import name.pachler.nio.file.WatchEvent;
import java.util.HashMap;
import java.util.UUID;
import name.pachler.nio.file.FileSystems;
import name.pachler.nio.file.Path;
import name.pachler.nio.file.WatchKey;
import name.pachler.nio.file.WatchService;
import platforms.PlatformSupport;
import sync.merge.MergeManager;
import static name.pachler.nio.file.StandardWatchEventKind.*;

/**
 *
 * @author paskud
 */
public class FileSystemSyncThread extends Thread {
    static WatchService watcher = FileSystems.getDefault().newWatchService();
    static Path dir;
    private boolean _enabled = true;
    private Map<WatchKey, String> _watchedFolders = new HashMap<WatchKey, String>();
    private MergeManager _manager;
    
    public FileSystemSyncThread(MergeManager manager) {
        _manager = manager;
    }
    
    /**
     * @return the _enabled
     */
    public boolean isEnabled() {
        return _enabled;
    }

    /**
     * @param aEnabled the _enabled to set
     */
    public void setEnabled(boolean aEnabled) {
        _enabled = aEnabled;
    }

    public void AddWatchPath(String path) {
        PlatformSupport.getPlatform().AddWatchPath(path, watcher, _watchedFolders);
    }
    
    
    @Override
    public void run() {
        AddWatchPath(ConfigurationManager.GetPreferences().getDefaultPhotoFolder());
        
        while (isEnabled()) {

            //wait for key to be signaled
            WatchKey key;
            try {
                key = watcher.take();
            } catch (InterruptedException x) {
                return;
            }

            if (!_enabled ) break;
            
            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent.Kind<?> kind = event.kind();

                //The filename is the context of the event.
                WatchEvent<Path> ev = (WatchEvent<Path>) event;
                Path filename = ev.context();
                File photoFile = new File(_watchedFolders.get(key), filename.toString());
                
                if (filename.toString().startsWith(".")) continue;

                if(kind == ENTRY_MODIFY || kind == ENTRY_DELETE){
                    if (filename.toString().toLowerCase().endsWith("jpg") || filename.toString().toLowerCase().endsWith("jpeg") || filename.toString().toLowerCase().endsWith("png") || filename.toString().toLowerCase().endsWith("gif")) {
                        _manager.merge(photoFile.getPath());
                        System.out.println("Event " + event.kind().name() + " for " + photoFile.getPath());                    
                    }
                } else if (kind == OVERFLOW) {
                    continue;
                }

                
            }

            //Reset the key -- this step is critical if you want to receive
            //further watch events. If the key is no longer valid, the directory
            //is inaccessible so exit the loop.
            boolean valid = key.reset();
            if (!valid) {
                break;
            }
        }
    }

    
}
